using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using RssReader.Data;
using RssReader.Data.DatabaseTableAdapters;

namespace RssReader
{
	[MonoTouch.Foundation.Register("FeedTableViewController")]
	public partial class FeedTableViewController : UITableViewController
	{
		static NSString CellID = new NSString ("FeedCellID");
		Database.FeedsRow[] AvailableFeeds;

		//
		// Constructor invoked from the NIB loader
		//
		public FeedTableViewController (IntPtr p) : base(p)
		{
			Title = "Feeds";
		}

		public FeedTableViewController () : base()
		{
			Title = "Feeds";
		}

		//
		// The data source for our TableView
		//
		class DataSource : UITableViewDataSource
		{
			FeedTableViewController tvc;

			public DataSource (FeedTableViewController tvc)
			{
				this.tvc = tvc;
			}

			public override int RowsInSection (UITableView tableView, int section)
			{
				return tvc.AvailableFeeds.Length;
			}
			
			public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
			{
				FeedsTableAdapter adapter = new FeedsTableAdapter();
				tvc.AvailableFeeds[sourceIndexPath.Row].DisplayOrder=destinationIndexPath.Row;
				if(destinationIndexPath.Row < sourceIndexPath.Row)
					tvc.AvailableFeeds[destinationIndexPath.Row].DisplayOrder=tvc.AvailableFeeds[destinationIndexPath.Row].DisplayOrder+1;
				else
					tvc.AvailableFeeds[destinationIndexPath.Row].DisplayOrder=tvc.AvailableFeeds[destinationIndexPath.Row].DisplayOrder-1;
					
				//adapter.Update(tvc.AvailableFeeds[sourceIndexPath.Row]);
				//adapter.Update(tvc.AvailableFeeds[destinationIndexPath.Row]);
			}

			
			public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
			{
				return true;
			}

			public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
			{
				var cell = tableView.DequeueReusableCell (CellID);
				if (cell == null) {
					cell = new UITableViewCell (UITableViewCellStyle.Default, CellID);
					cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
				}
				cell.ShowsReorderControl=true;
				Database.FeedsRow feed = tvc.AvailableFeeds[indexPath.Row];
				cell.TextLabel.Text = feed.FeedTitle;
				
				cell.TextLabel.Tag = (int)feed.FeedID;
				// Customize the cell here...
				
				return cell;
			}
		}

		//
		// This class receives notifications that happen on the UITableView
		//
		class TableDelegate : UITableViewDelegate
		{
			FeedTableViewController tvc;

			public TableDelegate (FeedTableViewController tvc)
			{
				this.tvc = tvc;
			}

			public override MonoTouch.UIKit.UITableViewCellEditingStyle EditingStyleForRow (UITableView tableView, NSIndexPath indexPath)
			{
				Console.WriteLine ("EditingStyleForRow " + indexPath.Row);
				return UITableViewCellEditingStyle.Delete;
			}
			public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)
			{
				base.AccessoryButtonTapped (tableView, indexPath);
			}

			ArticlesTableViewController articlesViewController;
			public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
			{
				articlesViewController = new ArticlesTableViewController ();
				Database.FeedsRow feed = tvc.AvailableFeeds[indexPath.Row];
				articlesViewController.CurrentFeed = feed;
				tvc.NavigationController.PushViewController (articlesViewController, true);
			}
			
		}

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			DataBind ();
			NavigationItem.RightBarButtonItem = EditButtonItem;
			TableView.Delegate = new TableDelegate (this);
			TableView.DataSource = new DataSource (this);
		}
		public Database.SitesRow CurrentSite { get; set; }
		public void DataBind ()
		{
			FeedsTableAdapter adapter = new FeedsTableAdapter ();
			Database.FeedsDataTable dt = new Database.FeedsDataTable ();
			adapter.Fill (dt);
			Title = CurrentSite.SiteTitle;
			var query = from t in dt.Where (r => r.SiteID == CurrentSite.SiteID) orderby t.DisplayOrder select t;
			AvailableFeeds= query.ToArray();
			
		}
	}
}
